package com.offcn.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.offcn.item.feign.PageFeign;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.List;

@CanalEventListener
public class CanalPageEventListener {

    @Autowired
    private PageFeign pageFeign;

    @ListenPoint(destination = "example",schema = "dongyimaidb",table = {"tb_goods"},
            eventType = {CanalEntry.EventType.UPDATE,CanalEntry.EventType.DELETE})
    public void onEventPage(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        // 如果是删除操作
        if(eventType == CanalEntry.EventType.DELETE){
            // 获取goodsId
            List<CanalEntry.Column> columns = rowData.getBeforeColumnsList();
            for (CanalEntry.Column column : columns) {
                if (column.getName().equals("id")) {
                    String goodsId = column.getValue();

                }
                //删除文件;

            }
        }else if(eventType == CanalEntry.EventType.UPDATE) { // 修改操作
            // 获取修改之前的数据
            List<CanalEntry.Column> beforeColumns = rowData.getBeforeColumnsList();
            for (CanalEntry.Column beforeColumn : beforeColumns) {
                if(beforeColumn.getName().equals("audit_status")){
                    if(beforeColumn.getValue().equals("1")){
                        System.out.println("不是审核通过的动作");
                        return ; // 不是审核的动作
                    }
                }
            }
            List<CanalEntry.Column> columns = rowData.getAfterColumnsList();
            for (CanalEntry.Column column : columns) {
                // 修改的状态必须是1 表示审核通过
                if(column.getName().equals("audit_status")){
                    if (!column.getValue().equals("1")) {
                        System.out.println("审核没有通过");
                        return ;
                    }
                }
                // 修改之前的状态不是1，修改之后的状态1，审核动作
                if (column.getName().equals("id")) {
                    String goodsId = column.getValue();
                    System.out.println("被审核通过商品Id:" + goodsId);
                    pageFeign.createPage(Long.parseLong(goodsId));
                }

            }
        }


    }
}
